/**
 *  ward @ 2019
 *  harrydeep@wardyan.top
 */
package top.wardyan.wdev.dbdevassist.action.base;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;

import top.wardyan.action.base.AbstractShell;
import top.wardyan.conf.Const;
import top.wardyan.control.WButton;
import top.wardyan.control.WComposite;
import top.wardyan.interfaces.Resizeable;
import top.wardyan.util.ShellUtil;
import top.wardyan.wdev.dbdevassist.action.dic.DicShell;
import top.wardyan.wdev.dbdevassist.action.func.FuncShell;
import top.wardyan.wdev.dbdevassist.action.name.NameShell;
import top.wardyan.wdev.dbdevassist.action.proc.ProcShell;
import top.wardyan.wdev.dbdevassist.action.table.TableShell;
import top.wardyan.wdev.dbdevassist.action.tabletype.TableTypeShell;
import top.wardyan.wdev.dbdevassist.action.tf.TableFuncShell;
import top.wardyan.wdev.dbdevassist.action.trigger.TriggerShell;
import top.wardyan.wdev.dbdevassist.action.user.UserShell;
import top.wardyan.wdev.dbdevassist.action.view.ViewShell;
import top.wardyan.wdev.dbdevassist.config.DDA;

/**
 * 2020-02-13 22:35:01
 * @author ward 2019年3月9日
 */
public abstract class BaseShell<T extends BaseShell<T>> extends AbstractShell<T> implements Resizeable{

	protected WComposite topCom;
	protected DDA dda = DDA.getInstance();
	
	public BaseShell() {
		this(SWT.SHELL_TRIM);
	}
	
	public BaseShell(int style) {
		super(style);
		this.createContents();
	}
	
	
	/**设置系统标题*/
	protected final String getTitle() {
		return DDA.SYSTEMCONFIG.SystemName + " " + DDA.SYSTEMCONFIG.BuildVersion + " " + title();
	}
	
	protected abstract String title();


	private void createContents() {
		this.setLayout(new FormLayout());
		this.setSize(1024, 720);
		ShellUtil.center(this);
		
		topCom = new WComposite(this, SWT.BORDER);
		topCom.setLayout(new FormLayout());
		FormData fd_topCom = new FormData();
		//fd_topCom.height = 720;
		//fd_topCom.width = 1024;
		fd_topCom.bottom = new FormAttachment(0, 70);
		fd_topCom.right = new FormAttachment(100, -1);
		fd_topCom.top = new FormAttachment(0);
		fd_topCom.left = new FormAttachment(0);
		topCom.setLayoutData(fd_topCom);
		//FormLayout topLayout = new FormLayout();
		
		//topCom.setLayout(topLayout);
		int height = 48, space = 6, pad = 14;
		// 这个按钮不需要子类修改
		WButton btnDic = new WButton(topCom, SWT.NONE, "数据字典", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.switchMenu(new DicShell());
			}
		});
		FormAttachment btnHAttachment = new FormAttachment(0, 58);
		FormData fd_btnDic = new FormData();
		fd_btnDic.bottom = btnHAttachment;
		fd_btnDic.right = new FormAttachment(btnDic, pad * 6);
		fd_btnDic.top = new FormAttachment(topCom, 10);
		fd_btnDic.left = new FormAttachment(topCom, 10);
		btnDic.setLayoutData(fd_btnDic);
		if(this.getText().endsWith("数据字典")) {
			btnDic.setBackground(Const.COLOR_GRAY);
		}
		
		WButton btnName = new WButton(topCom, SWT.NONE, "命名", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.switchMenu(new NameShell());
			}
		});
		FormData fd_btnName = new FormData();
		fd_btnName.bottom = btnHAttachment;
		fd_btnName.right = new FormAttachment(btnName, pad * 4);
		fd_btnName.top = new FormAttachment(topCom, 10);
		fd_btnName.left = new FormAttachment(btnDic, 10);
		btnName.setLayoutData(fd_btnName);
		if(this.getText().endsWith("命名")) {
			btnName.setBackground(Const.COLOR_GRAY);
		}
		
		
		WButton btnUser = new WButton(topCom, SWT.NONE, "用户", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.switchMenu(new UserShell());
			}
		});
		FormData fd_btnUser = new FormData();
		fd_btnUser.bottom = btnHAttachment;
		fd_btnUser.right = new FormAttachment(btnUser, pad * 4);
		fd_btnUser.top = new FormAttachment(topCom, 10);
		fd_btnUser.left = new FormAttachment(btnName, 10);
		btnUser.setLayoutData(fd_btnUser);
		if(this.getText().endsWith("用户")) {
			btnUser.setBackground(Const.COLOR_GRAY);
		}
		
		WButton btnTable = new WButton(topCom, SWT.NONE, "数据表", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.switchMenu(new TableShell());
			}
		});
		FormData fd_btnTable = new FormData();
		fd_btnTable.bottom = btnHAttachment;
		fd_btnTable.right = new FormAttachment(btnTable, pad * 5);
		fd_btnTable.top = new FormAttachment(topCom, 10);
		fd_btnTable.left = new FormAttachment(btnUser, 10);
		btnTable.setLayoutData(fd_btnTable);
		if(this.getText().endsWith("数据表")) {
			btnTable.setBackground(Const.COLOR_GRAY);
		}
		
		WButton btnProc = new WButton(topCom, SWT.NONE, "存储过程", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.switchMenu(new ProcShell());
			}
		});
		FormData fd_btnProc = new FormData();
		fd_btnProc.bottom = btnHAttachment;
		fd_btnProc.right = new FormAttachment(btnProc, pad * 6);
		fd_btnProc.top = new FormAttachment(topCom, 10);
		fd_btnProc.left = new FormAttachment(btnTable, 10);
		btnProc.setLayoutData(fd_btnProc);
		if(this.getText().endsWith("存储过程")) {
			btnProc.setBackground(Const.COLOR_GRAY);
		}
		
		// 数据表 存储过程 视图 表值函数 表类型 标量函数 触发器
		
		WButton btnView = new WButton(topCom, SWT.NONE, "视图", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.switchMenu(new ViewShell());
			}
		});
		FormData fd_btnView = new FormData();
		fd_btnView.bottom = btnHAttachment;
		fd_btnView.right = new FormAttachment(btnView, pad * 4);
		fd_btnView.top = new FormAttachment(topCom, 10);
		fd_btnView.left = new FormAttachment(btnProc, 10);
		btnView.setLayoutData(fd_btnView);
		if(this.getText().endsWith("视图")) {
			btnView.setBackground(Const.COLOR_GRAY);
		}
		
		WButton btnTableFunc = new WButton(topCom, SWT.NONE, "表值函数", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.switchMenu(new TableFuncShell());
			}
		});
		FormData fd_btnTableFunc = new FormData();
		fd_btnTableFunc.bottom = btnHAttachment;
		fd_btnTableFunc.right = new FormAttachment(btnTableFunc, pad * 6);
		fd_btnTableFunc.top = new FormAttachment(topCom, 10);
		fd_btnTableFunc.left = new FormAttachment(btnView, 10);
		btnTableFunc.setLayoutData(fd_btnTableFunc);
		if(this.getText().endsWith("表值函数")) {
			btnTableFunc.setBackground(Const.COLOR_GRAY);
		}
		WButton btnFunc = new WButton(topCom, SWT.NONE, "标量函数", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.switchMenu(new FuncShell());
			}
		});
		FormData fd_btnFunc = new FormData();
		fd_btnFunc.bottom = btnHAttachment;
		fd_btnFunc.right = new FormAttachment(btnFunc, pad * 6);
		fd_btnFunc.top = new FormAttachment(topCom, 10);
		fd_btnFunc.left = new FormAttachment(btnTableFunc, 10);
		btnFunc.setLayoutData(fd_btnFunc);
		if(this.getText().endsWith("标量函数")) {
			btnFunc.setBackground(Const.COLOR_GRAY);
		}
		
		WButton btnTableType = new WButton(topCom, SWT.NONE, "表类型", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.switchMenu(new TableTypeShell());
			}
		});
		FormData fd_btnTableType = new FormData();
		fd_btnTableType.bottom = btnHAttachment;
		fd_btnTableType.right = new FormAttachment(btnTableType, pad * 5);
		fd_btnTableType.top = new FormAttachment(topCom, 10);
		fd_btnTableType.left = new FormAttachment(btnFunc, 10);
		btnTableType.setLayoutData(fd_btnTableType);
		if(this.getText().endsWith("表类型")) {
			btnTableType.setBackground(Const.COLOR_GRAY);
		}
		
		WButton btnTrigger = new WButton(topCom, SWT.NONE, "触发器", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.switchMenu(new TriggerShell());
			}
		});
		FormData fd_btnTrigger = new FormData();
		fd_btnTrigger.bottom = btnHAttachment;
		fd_btnTrigger.right = new FormAttachment(btnTrigger, pad * 5);
		fd_btnTrigger.top = new FormAttachment(topCom, 10);
		fd_btnTrigger.left = new FormAttachment(btnTableType, 10);
		btnTrigger.setLayoutData(fd_btnTrigger);
		if(this.getText().endsWith("触发器")) {
			btnTrigger.setBackground(Const.COLOR_GRAY);
		}
//		var types = dda.typeService.available();
//		if(types != null) {
//			WButton pre = null;
//			for(YTType t : types) {
//				if(pre == null) {
//					pre = btnUser;
//				}
//				WButton btn = new WButton(topCom, SWT.NONE, t.getXName(), null);
//				if(this.getText().endsWith(t.getXName())) {
//					btn.setBackground(Const.COLOR_GRAY);
//				}
//				btn.addSelectionListener(new SelectionAdapter() {
//
//					@Override
//					public void widgetSelected(SelectionEvent e) {
//						new ProcShell().go();
//					}
//					
//				});
//				FormData fd_btn = new FormData();
//				fd_btn.bottom = btnHAttachment;
//				fd_btn.right = new FormAttachment(btn, t.getXName().length() * pad + pad * 2);
//				fd_btn.top = new FormAttachment(topCom, 10);
//				fd_btn.left = new FormAttachment(pre, 10);
//				btn.setLayoutData(fd_btn);
//				pre = btn;
//			}
//		}
		this.setMaximized(true);
	}
	
	
	protected void switchMenu(BaseShell<? extends BaseShell<?>> target) {
		Rectangle rec = self.getBounds();
		boolean max = self.getMaximized();
		self.dispose();
		target.setBounds(rec);
		target.setMaximized(max);
		target.go();
	}
}
